"""
utils.py
Created by Gleicon Moraes on 2009-03-02.

Set of classes and methods for bayes.py

"""
import os, glob
import nltk

class PTTokenizer:
    """Simple tokenizer for pt_BR, using stemming , stopwords and normalization
    """
    import re


    WORD_RE = re.compile('\\w+', re.U)

    def __init__(self, lower=False):
        self.lower = lower
        self.stemmer = nltk.RSLPStemmer()        
        s = nltk.corpus.stopwords.words('portuguese') #.decode('latin-1')
        self.stopwords = [a.decode('latin-1') for a in s]

    def tokenize(self, obj):
        for match in self.WORD_RE.finditer(obj):
		bigo = match.group().lower()
                if bigo not in self.stopwords:  # decode('latin-1') ?
                    yield self.stemmer.stem(bigo)


class Usage(Exception):
	def __init__(self, msg):
		self.msg = msg

class ClassifyUtils:
	def __init__(self, guesser):
		self.guesser=guesser

	def getbody(self, filename):
		try:
			f = open (filename, "r")
			b = f.read().decode('UTF-8')
			f.close()
			return b
		except IOError:
			print "File not found"	
			return None
		
	def classify(self, db, body):
		b = self.getbody(body)
		if b != None:
			value = self.guesser.guess(b)
			return value
		else:
			print "Error classifying for "+body
			return None

	def train_dir(self, db, tag, folder):
		allfiles = glob.glob(os.path.join(folder, '*.txt'))
		for infile in allfiles:
			self.train_tag(db, tag, infile)
		print "Trained "+str(len(allfiles))+" files"

	def train_tag(self, db, tag, body):
		b = self.getbody(body)
		if b != None:
			#print "training file: "+body+ " for tag "+tag+ " size: "+str(len(b))
			self.guesser.train(tag, b)
		else:
			print "Error training "+ body +" for tag: "+tag
		
